package pub.developers.forum.portal.controller.admin;

import org.springframework.web.bind.annotation.*;
import pub.developers.forum.api.model.PageRequestModel;
import pub.developers.forum.api.model.PageResponseModel;
import pub.developers.forum.api.model.ResultModel;
import pub.developers.forum.api.request.AdminBooleanRequest;
import pub.developers.forum.api.request.config.ConfigAddRequest;
import pub.developers.forum.api.request.config.ConfigPageRequest;
import pub.developers.forum.api.request.config.ConfigUpdateRequest;
import pub.developers.forum.api.response.config.ConfigResponse;
import pub.developers.forum.api.response.data.LastMonthData;
import pub.developers.forum.api.response.data.YestData;
import pub.developers.forum.api.service.ConfigApiService;
import pub.developers.forum.common.constant.Constant;
import pub.developers.forum.portal.support.WebUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/admin-rest/config") //管理员轮播图配置

public class AdminRestConfigController {

    @Resource
    private ConfigApiService configApiService;


    /*
    新增轮播图
    {
        "content": "{\"imgUrl\":\"imageUrl\",\"actionUrl\":\"http://www.baidu\"}",//轮播图内容 imageUrl:图片路径 actionUrl:图片跳转链接
        "endAt": "2023-10-12T16:00:00.000Z", //轮播图结束时间
        "name": "测试12", //轮播图名称
        "startAt": "2023-10-11T16:00:00.000Z", //轮播图开始时间
        "type": "HOME_CAROUSEL" //轮播图类型
    }
     */

    @PostMapping("/add")
    public ResultModel add(@RequestBody ConfigAddRequest addRequest, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return configApiService.add(addRequest);
    }

    //修改轮播图
    @PostMapping("/update")
    public ResultModel update(@RequestBody ConfigUpdateRequest updateRequest, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return configApiService.update(updateRequest);
    }



    /*
    轮播图配置操作:轮播图审核是否通过
     */

    @PostMapping("/state")
    public ResultModel state(@RequestBody AdminBooleanRequest booleanRequest, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return configApiService.state(booleanRequest);
    }


    /*
    轮播图配置页面的分页查询
    PageRequestModel<ConfigPageRequest> pageRequestModel
    {pageSize: 10, pageNo: 1, filter: {state: "PASS"//轮播图审核状态, name: null//轮播图名称, type: "HOME_CAROUSEL"}//轮播图类型}
     */
    @PostMapping("/page")
    public ResultModel<PageResponseModel<ConfigResponse>> page(@RequestBody PageRequestModel<ConfigPageRequest> pageRequestModel, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return configApiService.page(pageRequestModel);
    }

    //管理员删除轮播图
    @GetMapping("deleteConfig")
    public ResultModel deleteConfig(HttpServletRequest request , Long id)
    {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));
        return configApiService.deleteConfig(id);
    }

    //管理员统计昨日数据
    @PostMapping("newAddData")
    public ResultModel<YestData> newAddData(HttpServletRequest request)
    {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));
        return configApiService.newAddData();
    }

    //管理员统计近一个月的数据
    @GetMapping("monthData/{type}")
    public ResultModel<List<LastMonthData>> monthData(HttpServletRequest request , @PathVariable String type)
    {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));
        return configApiService.monthData(type);
    }
}
